﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Collections.Generic;
using ExtremeML.Packaging;
using NUnit.Framework;

namespace ExtremeML.Tests
{
	[TestFixture]
	public class AddDeleteWorksheets
	{
		[Test]
		public void ModifyWorksheetNames()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				foreach (var part in package.WorkbookPart.WorksheetParts)
				{
					var original = part.Name;
					part.Name += "_modified";
					Assert.AreEqual(original + "_modified", part.Name, "Modified worksheet name");
				}
			}
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void VerifyWorksheetNameUniquenessEnforced()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var worksheets = new List<WorksheetPartWrapper>(package.WorkbookPart.WorksheetParts);
				var worksheet = worksheets[0];
				worksheet.Name = "Sheet1"; // throws ArgumentException
			}
		}

		[Test]
		public void VerifyFirstWorksheetName()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				Assert.AreEqual("Sheet3", package.WorkbookPart.WorksheetParts[0].Name, "Worksheet rename.");
			}
		}

		[Test]
		public void AddNewWorksheet()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				Assert.AreEqual(package.WorkbookPart.WorksheetParts.Count, 3, "Worksheet count.");
				var worksheet = package.WorkbookPart.WorksheetParts.Add("Sheet4");
				Assert.AreEqual(4, package.WorkbookPart.WorksheetParts.Count, "Worksheet count.");
				Assert.AreEqual("Sheet4", package.WorkbookPart.WorksheetParts[3].Name, "Worksheet rename.");
			}
		}

		[Test]
		public void DeleteWorksheet()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				Assert.AreEqual(3, package.WorkbookPart.WorksheetParts.Count, "Worksheet count.");
				package.WorkbookPart.WorksheetParts.Delete("Sheet1");
				Assert.AreEqual(2, package.WorkbookPart.WorksheetParts.Count, "Worksheet count.");
				Assert.AreEqual(2, package.WorkbookPart.Workbook.Sheets.Sheets.Count, "Worksheet relationship element count.");
			}
		}

		[Test]
		public void DeleteAllExceptOneWorksheets()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				while (package.WorkbookPart.WorksheetParts.Count > 1)
				{
					var worksheet = package.WorkbookPart.WorksheetParts[0];
					package.WorkbookPart.WorksheetParts.Delete(worksheet.Name);
				}
				Assert.AreEqual(1, package.WorkbookPart.WorksheetParts.Count, "Worksheet count.");
			}
		}

		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void DeleteAllWorksheets()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				while (package.WorkbookPart.WorksheetParts.Count > 0)
				{
					var worksheet = package.WorkbookPart.WorksheetParts[0];
					package.WorkbookPart.WorksheetParts.Delete(worksheet.Name);
				}
			}
		}
	}
}
